<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="routeproductbomList">
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="汇报单编号" align="center" prop="batchReportCode" />
      <el-table-column label="工单编号" align="center" prop="workorderCode" />
      <el-table-column label="物料编码" align="center" prop="productCode" />
      <el-table-column label="物料名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="productSpc" />
      <el-table-column label="产线名称" width="100" align="center" prop="lineName" />
      <el-table-column label="生产数量" width="80" align="center" prop="quantity" />
      <el-table-column label="完成数量" width="80" align="center" prop="reportQuantity" />
      <el-table-column label="单位" width="80" align="center" prop="unitOfMeasure" />
      <el-table-column label="批次号" width="80" align="center" prop="batchCode" />
      <el-table-column label="质检状态" width="80" align="center" prop=" status">
        <template slot-scope="scope">
          <span>{{
            scope.row.status == "1"
              ? "未质检"
              : scope.row.status == "2"
                ? "已质检"
                : "--"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合格数量" align="center" prop="qualifiedQuantity" />
      <el-table-column label="不合格数量" align="center" prop="unqualifiedQuantity" />
      <el-table-column label="操作" width="130px" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleWriteBack(scope.row)" v-hasPermi="['mes:k3cloud:MorptmData:saveToErp']" v-if="scope.row.status == '2' && scope.row.jdyReportId == null">上传ERP</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDelete(scope.row)" v-if="scope.row.warehousingStatus == '1'">生成入库单</el-button>
          <!-- 回写时需要的判断 scope.row.jdy_report_id != null -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSubmit(scope.row)" v-if="scope.row.submitStatus == '0'">报检</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->
  </div>
</template>

<script>
import {
  reportList,
  getGenerateProductrecpt,
  editSubmit,
  selectUserRole,
  morptmData,
} from "@/api/mes/pro/workorder";
export default {
  name: "reportForm",
  props: {
    routeId: null,
    productId: null,
    processId: null,
    optType: null,
    buttonType: null,
    workorderId: null,
  },
  dicts: ["mes_workorder_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品制程物料BOM表格数据
      routeproductbomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        workorderId: this.workorderId,
        // pageNum: 1,
        // pageSize: 10
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    workorderId: function (val) {
      this.workorderId = val;
      this.queryParams.workorderId = val;
      if (val) {
        this.getList();
      }
    },
  },
  methods: {
    /** 查询产品制程物料BOM列表 */
    getList() {
      if (!this.workorderId) {
        this.loading = false;
        this.routeproductbomList = []
        console.log('未获取到workorderId')
        return
      }
      this.loading = true;
      reportList(this.workorderId).then((response) => {
        this.routeproductbomList = response.data || [];
        // this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleDelete(row) {
      this.$modal
        .confirm(
          '是否确认汇报单编号为"' + row.batchReportCode + '"的数据生成入库单？'
        )
        .then(function () {
          return getGenerateProductrecpt(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("生成成功");
        })
        .catch(() => { });
    },
    //会写汇报单
    handleWriteBack(row) {
      console.log(row, "汇报单参数");
      morptmData(row).then((res) => {
        if (res.code == 200) {
          this.getList()
          this.$message(res.msg);
        }
      });
    },
    handleSubmit(row) {
      selectUserRole("生产汇报单_生产组长")
        .then((res) => {
          this.$modal
            .confirm(
              '是否确认提交汇报单编号为"' + row.batchReportCode + '"的数据？'
            )
            .then(function () {
              return editSubmit(row);
            })
            .then(() => {
              this.getList();
              this.$modal.msgSuccess("提交成功");
            })
            .catch(() => { });
        })
        .catch(() => {
          console.log("qqqqqqqqq");
          return "false";
        });
    },
  },
};
</script>
